//------------------------------------------------------------------------------
//
//  File: AppleList.cpp
//
//  Copyright (C) 2007 Visual.Wei
//  Author: Wei Shuai <cpuwolf@sina.com>
//
//  Common Class for Device Driver Development
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
//
#include "AppleList.h"

CAppleList::CAppleList(HDC hdc,RECT * prect):LINE_HIGHT(40),FONT_OFFSET(5)
{
	HFONT m_hfont;
	LOGFONT m_log;
	//
	//prepare GDI
	m_hfont=(HFONT)GetCurrentObject(hdc,OBJ_FONT);
	GetObject(m_hfont,sizeof(m_log),&m_log);
	m_log.lfQuality=CLEARTYPE_QUALITY;
	m_log.lfHeight=20;
	m_log.lfWeight=FW_THIN;
	hrightfont=CreateFontIndirect(&m_log);
	m_log.lfWeight=FW_BOLD;
	hleftfont=CreateFontIndirect(&m_log);
	hpen=CreatePen(PS_SOLID,0,RGB(120,120,255));
	hbrbg=(HBRUSH)GetStockObject(WHITE_BRUSH);
	//copy rectange
	CopyRect(&rect,prect);
	rect.bottom=rect.top+LINE_HIGHT;
	CopyRect(&fontrect,&rect);
	
	InflateRect(&fontrect,-FONT_OFFSET,0);
	//
	m_Iter = mylist.begin( );
}

CAppleList::~CAppleList(void)
{
	DeleteObject(hleftfont);
	DeleteObject(hpen);
	DeleteObject(hrightfont);
}

void CAppleList::Paint(HDC m_hdc)
{
	HBRUSH holdbr;
	HPEN holdpen;
	HFONT holdfont;
	RECT tmprec;
	list <struct vlist>::iterator Iter;
	int i;

	//set pen mode
	SetBkMode(m_hdc,TRANSPARENT);
	//back DC
	holdbr=(HBRUSH)SelectObject(m_hdc,hbrbg);
	holdpen=(HPEN)SelectObject(m_hdc,hpen);
	holdfont=(HFONT)SelectObject(m_hdc,hleftfont);
	//draw background
	RoundRect(m_hdc,rect.left,rect.top,rect.right,rect.top+(LINE_HIGHT+1)*mylist.size()-1,10,10);
	//draw left font and lines
	CopyRect(&tmprec,&fontrect);
	for ( i=0,Iter = mylist.begin( ); Iter != mylist.end( ); Iter++,i++)
	{
		DrawText(m_hdc,Iter->left, Iter->leftlen,&tmprec,DT_LEFT|DT_VCENTER); 
		OffsetRect(&tmprec,0,LINE_HIGHT);
		if((i)!=mylist.size( )-1)
		{
			MoveToEx(m_hdc,tmprec.left-FONT_OFFSET,tmprec.top,NULL);
			LineTo(m_hdc,tmprec.right+FONT_OFFSET,tmprec.top);
		}
	}
	//draw right font
	SelectObject(m_hdc,hrightfont);
	SetTextColor(m_hdc,RGB(57,61,99));
	CopyRect(&tmprec,&fontrect);
	for ( Iter = mylist.begin( ); Iter != mylist.end( ); Iter++ )
	{
		DrawText(m_hdc,Iter->right, Iter->rightlen,&tmprec,DT_RIGHT|DT_VCENTER); 
		OffsetRect(&tmprec,0,LINE_HIGHT);
	}
	//restore DC
	SelectObject(m_hdc,holdbr);
	SelectObject(m_hdc,holdpen);
	SelectObject(m_hdc,holdfont);
}

BOOL CAppleList::Add(const TCHAR *l,const TCHAR *r)
{
	struct vlist ml;
	ml.left=l;
	ml.right=r;
	ml.leftlen=lstrlen(l);
	ml.rightlen=lstrlen(r);
	//m_Iter++;
	mylist.push_back( ml );
	
	return 0;
}
